//
//  RoyHud.m
//  uricacids
//
//  Created by zglee on 10/15/19.
//  Copyright © 2019 zglee. All rights reserved.
//

#import "RoyHud.h"
#import <Toast/Toast.h>
#import "StatusView.h"


typedef void(^complented)(BOOL result);
static RoyHud* hud;

@interface RoyHud ()
//@property (nonatomic) UIWindow *alertWindow;
@end

@implementation RoyHud


#pragma mark - toast wrapper
+ (void)showToast:(NSString *)toast {
    [StatusView showWithStatus:toast];
}
+ (void)showCenterToast:(NSString *)toast {
    [StatusView showCenterWithStatus:toast];
}
+ (void)showToast:(NSString *)toast complented:(complented)successed{
    [StatusView showWithStatus:toast complented:successed];
}

+ (void)showToast:(NSString *)toast
           OnView:(UIView *)view
{
    [view makeToast:toast
           duration:2.0f
           position:[NSValue valueWithCGPoint:view.center]];
}

#pragma mark - UIAlertController wrapper
+ (void)showDialogwithvc:(UIViewController *)vc
                   title:(NSString *)title
                 content:(NSString *)content
                 leftstr:(NSString *)leftstr
                lhandler:(void (^)(UIAlertAction *action))lhandler
                rightstr:(NSString *)rightstr
                rhandler:(void (^)(UIAlertAction *action))rhandler
{
    UIAlertAction *litem;
    UIAlertAction *ritem;
    if (![leftstr isEqualToString:@""]) {
        litem = [UIAlertAction actionWithTitle:leftstr
                                                style:UIAlertActionStyleDefault
                                              handler:lhandler];
    }
    
    if (![rightstr isEqualToString:@""]) {
        ritem =[UIAlertAction actionWithTitle:rightstr
                                                style:UIAlertActionStyleDefault
                                              handler:rhandler];
    }
    

    [self showDialogwithvc:vc
                     title:title
                   content:content
                     litem:litem
                     ritem:ritem];
}


+ (void)showDialogwithvc:(UIViewController *)vc
                   title:(NSString *)title
                 content:(NSString *)content
                   litem:(UIAlertAction *)litem
                   ritem:(UIAlertAction *)ritem
{
    UIAlertController *alt = [UIAlertController alertControllerWithTitle:title
                                                                 message:content preferredStyle:UIAlertControllerStyleAlert];

    if (litem) {
        [alt addAction:litem];
    }
    
    if (ritem) {
        [alt addAction:ritem];
    }
    
    [vc presentViewController:alt animated:true completion:nil];
}


+ (void)showsheetwithvc:(UIViewController *)vc
                  title:(NSString *)title
                content:(NSString *)content
                  items:(NSArray <UIAlertAction *> *)items {
    UIAlertController *alt = [UIAlertController alertControllerWithTitle:title
                                                                 message:content preferredStyle:UIAlertControllerStyleActionSheet];

    for (UIAlertAction *action in items) {
        [alt addAction:action];
    }
    
    [vc presentViewController:alt
                     animated:true
                   completion:nil];
}

- (UIWindow *)alertWindow
{
    return [UIApplication sharedApplication].keyWindow;
}

@end


@implementation RoyHudcontent

- (UIWindow *)alertWindow
{
    return [UIApplication sharedApplication].keyWindow;
}

- (void)show {
    [self.alertWindow addSubview:self];
//    self.alertWindow.hidden = false;
}

- (void)hide {
//     self.alertWindow.hidden = true;
     [self removeFromSuperview];
}

@end
